﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SportsStore.Domain.Abstract;
using SportsStore.Domain.Concrete;
using SportsStore.Domain.Entities;
using SportsStore.WebUI.Models;

namespace SportsStore.WebUI.Controllers
{
    
    public class DocumentController : Controller
    {
        private IDocumentRepository documentRepository;
        private IProductRepository productRepository;

        public DocumentController(IDocumentRepository  docRepo, IProductRepository proRepo)
        {
            documentRepository = docRepo;
            productRepository = proRepo;
        }

        //
        // GET: /Document belongs to certain product
        public ViewResult Index(int productId)
        {
            Product product = productRepository.Products.FirstOrDefault(p => p.Id == productId);
            
            //product.Documents = documentRepository.Documents.Where(d=>d.ProductId==productId).ToList(); //already use "include" in EFDocumentRepository. No need to bind manually
           
           // if (product.Documents.Count >0)
            //{
                DocumentIndexViewModel viewModel = new DocumentIndexViewModel
                    {
                        ProductId = product.Id,
                        ProductName = product.Name,
                        ProductCategory = product.Category.Name,
                        Documents = product.Documents
                    };

                return View(viewModel);
            //}
           
        }


       
        public FileContentResult GetDocument(int documentId)
        {
            Document document = documentRepository.Documents.FirstOrDefault(d => d.Id == documentId);
            if (document != null && document.DocumentData!=null)
            {
                return File(document.DocumentData, document.DocumentMimeType);
            }
            else
            {
                return null;
            }
        }
         


        [Authorize]
        public ViewResult AddDocument(int productId)
        {
            //ViewBag.productId = productId;
            return View(new Document{ProductId = productId});
        }

        [Authorize]
        [HttpPost]
        public ActionResult AddDocument(int productId, HttpPostedFileBase file)
        {
            Document document = new Document();
            document.ProductId = productId;
            if (file!=null)
            {
                document.DocumentData=new byte[file.ContentLength];
                file.InputStream.Read(document.DocumentData, 0, file.ContentLength);
                document.DocumentMimeType = file.ContentType;
                document.Name = file.FileName;
                document.Category = "Manual";//change later for different category.e.g. datasheet
                document.Date = DateTime.Today;
                
                    documentRepository.AddDocument(document);

                    TempData["message"] = string.Format("{0} has been saved", document.Name);
                    return RedirectToAction("Index",new{productId = document.ProductId});
            }
            else
            {
                return View(document);
            }
        }

        [Authorize]
        public RedirectToRouteResult DeleteDocument(int documentId)
        {
            Document document = documentRepository.Documents.FirstOrDefault(d => d.Id == documentId);
            if (document != null)
            {
                documentRepository.DeleteDocument(document);
                TempData["message"] = string.Format("{0} has been deleted", document.Name);
            }
            else
            {
                TempData["message"] = string.Format("Failed to delete");
            }
            return RedirectToAction("Index", new{productId = document.ProductId});
        }
    }
}
